package com.example.group.network.express_plus_app.ui.home;



import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;


/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/3/2 13:47
 */
public class MyLocationListener implements AMapLocationListener {


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                amapLocation.getLatitude();//获取纬度
                amapLocation.getLongitude();//获取经度
                amapLocation.getAccuracy();//获取精度信息
                amapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                amapLocation.getCountry();//国家信息
                amapLocation.getProvince();//省信息
                amapLocation.getCity();//城市信息
                amapLocation.getDistrict();//城区信息
                amapLocation.getStreet();//街道信息
                amapLocation.getStreetNum();//街道门牌号信息
                amapLocation.getDescription();
                onAddressListener.onFindAddress(amapLocation.getDistrict());
                Log.i("地址",amapLocation.getAddress());
                Log.i("街道信息",amapLocation.getStreet());
                Log.i("街道门牌号信息",amapLocation.getStreetNum());
                Log.i("获取精度信息", String.valueOf(amapLocation.getAccuracy()));
                Log.i("地址描述", String.valueOf(amapLocation.getDescription()));
//可在其中解析amapLocation获取相应内容。
            }else {
                onAddressListener.onFindAddress("请打开GPS,点击刷新");
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }


    OnAddressListener onAddressListener;
    //初始化onAddressListener
    public void setOnAddressListener(OnAddressListener addressListener){
        onAddressListener=addressListener;
    }

}
